//
// Created by xili on 2024/8/27 18:42.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int maximumUniqueSubarray(vector<int> &nums) {
        int ans = 0;
        unordered_map<int, int> ump;
        int begin = 0;
        int sum = 0, n = nums.size();
        for (int i = 0; i < n; i++) {
            //当前数已经在map中出现过了，将这个数到begin的数在sum和map中全部删除
            if (ump.count(nums[i])) {
                int t = begin;
                begin = ump[nums[i]] + 1;
                for (int j = t; j <= ump[nums[i]]; j++) {
                    sum -= nums[j];
                    ump.erase(nums[j]);
                }
            }
            sum += nums[i];
            ump[nums[i]] = i;
            ans = max(ans, sum);
        }
        return ans;
    }
};